import MySQLdb,os,re,time,datetime,threading
start_time = time.time()
bat_run_over = 666
def bat_run():
    #os.system('update.bat')
    print('bat开始运行了')
    time.sleep(10)
    print("bat运行结束了")
bat_open = threading.Thread(target=bat_run)
bat_open.start()
officehome = input('请输入办公室门牌号：')
name = input('请输入使用人名字：')
while True: 
    print(threading.active_count())
    if threading.active_count() == 1:
        print('主线开始运行了')
        db = MySQLdb.connect("47.93.53.27","root","741852aa","ping_test_db",charset="utf8")    
        cursor = db.cursor()
        pc_peizi = open("mypc.txt","r").read()
        ## =============正则表达式 提取需要的数据上传数据库=============#
        ComputerName = re.search(r'Computer Name =  (.*?)\n',pc_peizi).group(1)#计算机名字
        OSType = re.search(r'OS       Type =  (.*?)\n',pc_peizi).group(1)#系统类型
        CPUModel = re.search(r'CPU     Model =  (.*?)\n',pc_peizi).group(1)#CPU型号
        RAMSize = re.search(r'RAM      Size =  (.*?) RAM\n',pc_peizi).group(1)#内存
        DisplayCard = re.search(r'Display  Card =  (.*?)\n',pc_peizi).group(1) #显卡
        Manufacturer = re.search(r'Manufacturer     = (.*?)\n',pc_peizi).group(1)#主板
        MACAddress = re.search(r'MAC Address =  (.*?)\n',pc_peizi).group(1)#MAC地址
        IPAddress = re.search(r'IP Address =  (.*?) \n',pc_peizi).group(1)#IP
        USB = re.search(r'USB is (.*?)\n',pc_peizi).group(1)#USB是否被ban
        today = datetime.datetime.today()#上传日期
        sql = 'replace into ping_test_app_pc_db values("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")' % (OSType,ComputerName,CPUModel,RAMSize,DisplayCard,Manufacturer,IPAddress,MACAddress,USB,today,officehome,name)
        try:
            cursor.execute(sql)
            db.commit()
            print("已提交,10秒后自动关闭窗口")
            #time.sleep(10)
        except:
            print('提交失败了，自己拍照登记吧')
            time.sleep(30)
        break
    else:
        print('请稍等...')
        time.sleep(3)
db.close()
stop_time = time.time() - start_time
print("进程结束","耗时",stop_time)


